package com.hns.can.master.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.CommAccDobPoitInfoEntity;
import com.hns.can.master.entity.CommBteryManageStmInfoEntity;
import com.hns.can.master.entity.CommDriveMdIdnxInfoEntity;
import com.hns.can.master.entity.CommDriveRcdInfoEntity;
import com.hns.can.master.entity.CommDriveStatInfoEntity;
import com.hns.can.master.entity.CommDrvSctBhvInfoEntity;
import com.hns.can.master.entity.CommEltMhyOprtInfoEntity;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.can.master.entity.CommIetaPassFlowInfoEntity;
import com.hns.can.master.entity.CommLogFileRcdEntity;
import com.hns.can.master.entity.CommMftAlmEntity;
import com.hns.can.master.entity.CommSpedTrajEntity;
import com.hns.can.master.service.EqmtFileImpService;
import com.hns.can.master.service.ImportCSVFileService;
import com.hns.tool.pub.FileUploadUtil;
import com.hns.tool.pub.UtilString;

/**
 * 设备文件导入控制类
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/eqmtFileImp")
public class EqmtFileImpController extends BaseController {
    
    @Autowired
    private ImportCSVFileService importCSVFileService; // 注入importCSVFileService
    @Autowired
    private EqmtFileImpService eqmtFileImpService; // 注入eqmtFileImpService
    
	/**
	 * @return
	 * @throws Exception
	 * @author:JeffXu
	 * @email:835264047@qq.com
	 * @创建日期:2014-10-8
	 * @功能说明：设备文件导入
	 */
//	@RequestMapping("/eqmtFileImpTab")
//	public ModelAndView eqmtFileImpTab() throws Exception {
//		return new ModelAndView("sysmag/eqmtFileImpTab");
//	}
	
	/**
	 * @param fileType
	 * @param eqmtFile
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author:JeffXu
	 * @email:835264047@qq.com
	 * @创建日期:2014-10-21
	 * @功能说明：导入设备文件数据
	 */
    @SuppressWarnings({"unchecked", "rawtypes" })
    @RequestMapping("/uploadEqmtFile")
    public Object uploadEqmtFile(
        @RequestParam(value = "fileType", required = true) String fileType,
        @RequestParam(value = "eqmtFile", required = true) MultipartFile eqmtFile,
                        HttpServletRequest request) throws Exception {
	    Map<String, Object> resData = new HashMap<String, Object>();
        String oldFileName = eqmtFile.getOriginalFilename();
        /*文件没导入成功，导入*/
        if(!eqmtFileImpService.checkFileIsImp(oldFileName)){
            CommLogFileRcdEntity logrcd = new CommLogFileRcdEntity(); // 设备文件导入的日志信息
            String targetPath = FileUploadUtil.getWebRoot(request) + "/template/temp/";
            String newFileName = UtilString.getUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
            String fileName = targetPath + newFileName; // 临时文件的完整路径
            try{
                FileUploadUtil.uploadFile(eqmtFile.getInputStream(), newFileName, targetPath); // 上传文件到临时文件
                /*导入数据*/
                Map<String, Object> dataMap = this.getDataMap();
                Object[] dataArray = (Object[]) dataMap.get(fileType);
                List<BaseEntity> list = (List<BaseEntity>) importCSVFileService.doImportCSVFile(
                            (Class)dataArray[0], fileName, dataArray[1].toString(), fileType);
                /*记录日志*/
                logrcd.setFileBeginTime(list.get(0).getRcrdTime());
                logrcd.setFileEndTime(list.get(list.size() - 1).getRcrdTime());
                logrcd.setFileSt("1");
                logrcd.setFileName(oldFileName);
                logrcd.setGenTime(new Date());
                logrcd.setFileType(fileType);
                eqmtFileImpService.saveLogFileRcdEntity(logrcd); //写入日志表
                /*导入数据成功*/
                resData.put("resMsg", oldFileName + "  -  设备数据导入成功！");
            }catch(Exception e){
                resData.put("resMsg", oldFileName + "  -  导入失败<br/>失败原因 ： "+e.getMessage());
                //throw new Exception(e.getMessage());
            }finally{
                FileUploadUtil.delFile(fileName); // 删除临时文件
            }
        }else{
            resData.put("resMsg", oldFileName + "  -  该文件已经导入过！");
        }
        return this.getReturnResult(resData);
    }
    
    /**
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-10-21
     * @功能说明：得到维护关系的map
     */
    private Map<String, Object> getDataMap(){
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("01", new Object[]{CommEnerConsStatInfoEntity.class,CommEnerConsStatInfoEntity.TABLENAME});
        dataMap.put("02", new Object[]{CommDrvSctBhvInfoEntity.class,CommDrvSctBhvInfoEntity.TABLENAME});
        dataMap.put("03", new Object[]{CommDriveMdIdnxInfoEntity.class,CommDriveMdIdnxInfoEntity.TABLENAME});
        dataMap.put("04", new Object[]{CommAccDobPoitInfoEntity.class,CommAccDobPoitInfoEntity.TABLENAME});
        dataMap.put("05", new Object[]{CommDriveRcdInfoEntity.class,CommDriveRcdInfoEntity.TABLENAME});
        dataMap.put("06", new Object[]{CommMftAlmEntity.class,CommMftAlmEntity.TABLENAME});
        dataMap.put("07", new Object[]{CommDriveStatInfoEntity.class,CommDriveStatInfoEntity.TABLENAME});
        dataMap.put("08", new Object[]{CommIetaPassFlowInfoEntity.class,CommIetaPassFlowInfoEntity.TABLENAME});
        dataMap.put("09", new Object[]{CommSpedTrajEntity.class,CommSpedTrajEntity.TABLENAME});
        dataMap.put("10", new Object[]{CommBteryManageStmInfoEntity.class,CommBteryManageStmInfoEntity.TABLENAME});
        dataMap.put("11", new Object[]{CommEltMhyOprtInfoEntity.class,CommEltMhyOprtInfoEntity.TABLENAME});
        return dataMap;
    }
    
}
